//
//  TRFocusViewController.m
//  WeiMiLan
//
//  Created by Apple on 15/11/26.
//  Copyright © 2015年 Mac. All rights reserved.
//

#import "TRFocusViewController.h"

@interface TRFocusViewController ()
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation TRFocusViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self initNavigation];
    
    // 1.注册
    UINib *nib1 = [UINib nibWithNibName:@"TRFansViewTableViewCell" bundle:nil];
    [self.tableView registerNib:nib1 forCellReuseIdentifier:@"TRFansViewTableViewCell"];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.contents=[[NSMutableArray alloc]init];
    self.contents=nil;
    [self addHeader];
    [self addFooter];
    NSLog(@"viewDidLoad");
    
}

- (void)initNavigation
{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.weChatNavigationBar=[[WeChatNavigationBar alloc] init];
    [self.view addSubview:self.weChatNavigationBar];
    self.weChatNavigationBar.titleLabel.text=@"我的关注";
    [self.weChatNavigationBar.leftButton addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchUpInside];
    self.weChatNavigationBar.rightButton.hidden = YES;
    NSLog(@"initNavigation");
}

- (void)exit
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)loadData:(int)num
{
    NSLog(@"loadData");
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setObject:QLIntToNumber(num) forKey:@"PAGE_NUM"];
    NSString *user=[[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    [dic setObject:user forKey:@"userId"];
    
    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [requestManager GET:@"http://ping.vsmu.cn/app/Interface/User?type=9006" parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if ([responseObject[@"MESSAGE_INFO"][@"CODE"] isEqualToString:@"0000"]) {
            if (num==1) {
                //第一页
                if ([responseObject[@"USER_INFO_RSP"][@"USER_INFOS"] isKindOfClass:[NSDictionary class]]) {
                    [self.contents addObject:responseObject[@"USER_INFO_RSP"][@"USER_INFOS"]];
                    NSLog(@"self.contents.count=%lu",(unsigned long)self.contents.count);
                }else{
                    self.contents = [NSMutableArray arrayWithArray:responseObject[@"USER_INFO_RSP"][@"USER_INFOS"]];
                    NSLog(@"self.contents.count=%lu",(unsigned long)self.contents.count);
                }
            }else{
                [self.contents addObjectsFromArray:responseObject[@"USER_INFO_RSP"][@"USER_INFOS"]];
            }
            
            NSDictionary *pageInfo = responseObject[@"PAGE_INFO"];
            self.nextPage = [pageInfo ql_intForKey:@"NEXT"];
            self.currentPage = [pageInfo ql_intForKey:@"NUMBER"];
            if (self.nextPage>self.currentPage) {
                refreshFooterView.isend = NO;
            }else{
                refreshFooterView.isend = YES;
            }
            
            [self performSelector:@selector(doneWithView:) withObject:nil afterDelay:2.0f];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:STR_MSG_NETWORK_ERROR];
        [self performSelector:@selector(doneWithView:) withObject:nil afterDelay:2.0f];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"numberOfRowsInSection");
    long count = self.contents==nil?0:self.contents.count;
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSLog(@"cellForRowAtIndexPath");
    TRFansViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TRFansViewTableViewCell"];
    if (cell == nil) {
        cell = [[TRFansViewTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TRFansViewTableViewCell"];
    }
    NSDictionary *info = [self.contents objectAtIndex:indexPath.row];
    cell.info = info;
    [cell.fllowBtn addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
    [cell setNeedsDisplay];
    return cell;
}
-(void)test{
    NSLog(@"test");
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSDictionary *dic = [self.contents objectAtIndex:indexPath.row];
    [self connectShe:dic];
}
- (UIWebView *)webView {
    if (_webView == nil) {
        _webView = [[UIWebView alloc]init];
    }
    return _webView;
}
- (void)connectShe:(NSDictionary *)userInfo
{
//    NSLog(@"connectShe");
//    NSDictionary *dic=[[NSDictionary alloc]init];
//    dic=userInfo;
//    NSLog(@"dic==%@",dic);
//    NSString *name=[dic objectForKey:@"NAME"];
//    if ([[userInfo allKeys] containsObject:@"PHONE"]) {
//        NSString *tel = userInfo[@"PHONE"];
//        NSString *telStr = [NSString stringWithFormat:@"tel://%@",tel];
//        NSURL *telUrl = [NSURL URLWithString:telStr];
//        [self.webView loadRequest:[NSURLRequest requestWithURL:telUrl]];
//    } else {
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"对不起对方未绑定手机号码，请先去填写 " delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];//bei 1125
//        [alert show];
//    }
    [QLPlayerManager openUserHomeWithId:[userInfo ql_intForKey:@"ID"] viewController:self];
    
}

- (void)addFooter
{
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self.tableView;
    refreshFooterView = footer;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [self loadData:self.nextPage];
    };
}

- (void)loadFirstData
{
    [refreshHeaderView beginRefreshing];
}

- (void)addHeader
{
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.tableView;
    refreshHeaderView = header;
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        [self loadData:1];
    };
    [self performSelector:@selector(loadFirstData) withObject:nil afterDelay:0.2];
}

- (void)doneWithView:(id)sender
{
    // 刷新表格
    [self.tableView reloadData];
    [refreshFooterView endRefreshing];
    [refreshHeaderView endRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
